package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.IEmployeeService;
import com.twentyOne.ClothingLand.service.impl.EmployeeService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.Employee;

public class EmployeeAction extends ActionSupport {

	private Employee employee;
	private int year;
	private int month;
	private int date;
	private IEmployeeService employeeService = new EmployeeService();

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	public String addEmployee() throws Exception {
		
		Timestamp hiredate = new Timestamp(new Date(year, month, date).getTime());
		employee.setHiredate(hiredate);
		this.employeeService.addEmployee(employee);
		return null;

	}

	public String authenticateEmployee() throws Exception {

		Employee employee_ = this.employeeService.authenticateEmployee(employee.getUserName(), employee.getPassword(), employee.getShopId());
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		if(employee_ != null) {
			employee_.setPassword("");
			String employeeJson = JsonUtil.objectToJson(employee_);
			out.print(employeeJson);
			Map session = ActionContext.getContext().getSession();
			session.put("user", employee_);
		}else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		
		return null;

	}
	
	public String authenticateAdmin() throws Exception {

		Employee employee_ = this.employeeService.authenticateAdmin(employee.getUserName(), employee.getPassword());
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		if(employee_ != null) {
			employee_.setPassword("");
			String employeeJson = JsonUtil.objectToJson(employee_);
			out.print(employeeJson);
			Map session = ActionContext.getContext().getSession();
			session.put("user", employee_);
		}else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		
		return null;

	}
	
	public String logout() throws Exception {
		
		Map session = ActionContext.getContext().getSession();
		Employee employee_ = (Employee) session.get("user");
		if (employee_ != null) {
			session.remove("user");
		}
		
		return null;
		
	}

}
